package ldh.im.fxgui.rpc.router;

import com.jfoenix.controls.JFXDialog;
import javafx.application.Platform;
import ldh.im.dto.business.av.AvInvitationReq;
import ldh.im.fxbase.data.event.AvInvite;
import ldh.im.fxbase.data.event.EventBusFactory;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.util.DialogUtil;
import ldh.im.fxbase.util.FxThreadUtil;
import ldh.im.fxbase.util.ImCacheFactory;
import ldh.im.fxgui.util.UiUtil;
import ldh.rpc.Rpc;
import ldh.rpc.RpcMapper;
import ldh.rpc.exception.RpcException;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 接受对方语音、视频邀请
 */
@Rpc
public class AvConsumerRouter {

    /**
     * 接受对方的语音、视频聊天邀请
     * @throws RpcException
     */
    @RpcMapper(path = "serverRedirectAvInvitation", asynHandle = true)
    public void receiveInvitation(AvInvitationReq avInvitationReq) throws RpcException {
        FxThreadUtil.runLater(()->{
            Map<String, Consumer<JFXDialog>> buttonsMap = new HashMap<>();
            buttonsMap.put("接受", dialog-> accept(avInvitationReq));
            buttonsMap.put("拒绝", dialog-> reject(avInvitationReq));
            DialogUtil.alert("语音邀请", "语音聊天", false, buttonsMap);
        });
    }

    private void accept(AvInvitationReq avInvitationReq) {
        String groupId = avInvitationReq.getGroupCode();
        String userId = avInvitationReq.getFromUserId();
        ImUser imUser = ImCacheFactory.getInstance().getUser(userId);
        UiUtil.getMainController().switchUserMessage(imUser, (Void)->{
            AvInvite avInvite = new AvInvite(avInvitationReq.getFromUserId(), userId, avInvitationReq.getChatType(),
                    groupId);
            EventBusFactory.getInstance().post(avInvite);
        });
    }

    private void reject(AvInvitationReq avInvitationReq) {

    }
}
